package cn.lentme.app.activity;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.EditText;

import androidx.annotation.Nullable;

import cn.lentme.app.App;
import cn.lentme.app.base.BaseActivity;
import cn.lentme.app.component.utils.LogUtil;
import cn.lentme.app.component.utils.OnPermissionListener;
import cn.lentme.app.consts.ConstPool;
import cn.lentme.app.state.ClientTransponder;
import cn.lentme.app.state.DynamicConfig;
import io.netty.util.internal.StringUtil;

public class SplashActivity extends PermissionActivity {
    protected static String TAG = ConstPool.TAG + "Splash";

    public static boolean SIGN_NETWORK = false;

    String[] permissions = { Manifest.permission.WRITE_EXTERNAL_STORAGE,//写权限
                            Manifest.permission.READ_EXTERNAL_STORAGE,//读权限
                            Manifest.permission.CAMERA, //相机权限
                            Manifest.permission.READ_PHONE_STATE//读取手机
                            };

    private Handler handler = new Handler();

    /**
     * 检查网络是否连接
     * @return
     */
    public static boolean checkedNetwork(BaseActivity baseActivity) {
        ConnectivityManager connectivityManager = (ConnectivityManager) baseActivity.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mobNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        NetworkInfo wifiNetworkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (!mobNetworkInfo.isConnected() && !wifiNetworkInfo.isConnected()) {
            baseActivity.show("你已经进入没有网络的异次元世界￣ ￣)σ");
            return false;
        }
        return true;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        //最首要的调试输出工具初始化
        LogUtil.loadPrinter(new LogUtil.myPrinter() {
            @Override
            public void print(String text) {
                Log.e("LogLentMe",text);
            }
        });

        reqPermission(permissions, new OnPermissionListener() {
            //请求软件所需权限
            @Override
            public void onGranted() {
                Init();
            }

            @Override
            public void onDenied() {
                show("没有给权限会影响app的功能运行哦!");
                Init();
            }
        });
    }

    private void setServer() {
        ClientTransponder transponder = ClientTransponder.getInstance();
        final View view = getLayoutInflater().inflate(R.layout.dialog_set_server, null);
        new AlertDialog.Builder(this)
                .setTitle("设置服务器")
                .setView(view)
                .setCancelable(false)
                .setPositiveButton("确认", (dialog, which) -> {
                    final EditText address = view.findViewById(R.id.dialog_edit_server_address);
                    final EditText port = view.findViewById(R.id.dialog_edit_server_port);
                    if(StringUtil.isNullOrEmpty(address.getText().toString()) ||
                            StringUtil.isNullOrEmpty(port.getText().toString())) {
                        transponder.showToast("请输入服务器ip和端口");
                        setServer();
                        return;
                    }
                    DynamicConfig.serverAddress = address.getText().toString();
                    DynamicConfig.serverPort = Integer.parseInt(port.getText().toString());
                    if(!transponder.connect()) {
                        setServer();
                        return;
                    }
                    transponder.showToast("连接成功!!!");
                    gotoActivity();
                })
                .show();
    }

    private void Init(){
        //APP初始化部分
        SIGN_NETWORK = checkedNetwork(this);
        if ( SIGN_NETWORK ){
            LogUtil.i(TAG,"网络连接成功");
            ClientTransponder transponder = ClientTransponder.getInstance();//实际上是在初始化
            if(transponder.isConnectFailed()) {
                transponder.showToast("服务器连接失败，正在重置!");
                setServer();
                return;
            }
            gotoActivity();
        }
        else {
            App.getInstance().showToast("网络未连接喔!!暂时使用不了本软件!");
            finish();
        }
    }

    private void gotoActivity(){
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                //startActivity(new Intent(SplashActivity.this, MainActivity.class));
                startActivity(new Intent(SplashActivity.this,LoginActivity.class));
                animFade();
                finish();
            }
        },2000);
        //可以拓展动画
    }

}
